import request from '@/utils/unirequest';
import type { LuchResponse } from '@/type/common';

interface BaseEntity {
  ID?: string | null;
  [key: string]: any;
}

interface BaseVm<T extends BaseEntity> {
  Entity: T;
  ActionName?: string | null;
  [key: string]: any;
}

interface BaseView extends BaseEntity {
  [key: string]: any;
}

export function createCrudApi<TView extends BaseView, TVm extends BaseVm<TEntity>, TEntity extends BaseEntity>(baseUrl: string) {
  return {
    async search(data: object): Promise<LuchResponse<TView[]>> {
      return await request.post(`${baseUrl}/Search`, data);
    },

    async get(id: string | number): Promise<TVm> {
      return await request.get(`${baseUrl}/${id}`);
    },

    async add(data: TVm): Promise<TEntity> {
      return await request.post(`${baseUrl}/Add`, data);
    },

    async edit(data: TVm): Promise<TEntity> {
      return await request.put(`${baseUrl}/Edit`, data);
    },

    async delete(ids: (string | number)[]): Promise<LuchResponse<number>> {
      return await request.post(`${baseUrl}/BatchDelete`, ids);
    }
  };
}